<?php
namespace App\Api\Helpers;

/**
 * 移动运营商
 *
 */
trait SendMessage{
    /**
     * 获取用户token
     *
     */
    public function getAccessToken(){
        $wxconfig = config("wechat.payment.default");
        //重新获取access_token,并全局缓存
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='
            .$wxconfig['app_id'].'&secret='.$wxconfig['secret']);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
        //获取access_token
        $data = json_decode(curl_exec($curl),true);
        if($data != null && $data['access_token']){
            //返回access_token
            return $data['access_token'];
        }else{
            return false;
        }
    }


    //curl使用post方式请求url,参数为$arr是post方式传送的数据,为数组类型,$url为需要请求的url
    public function curl_post($arr,$url){
        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        //设置post数据
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($arr));
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        return $data;
    }

    //发送模版消息
    // 购买成功  RnWD8sDGTeH99RO_b4XYJC3gSreCBTgVV__zsAeien8
    public function sendTemplateMessage($openid, $template_id, $data_params){
        if ( !$openid || !$template_id ) {
            return false;
        }

        //获取access_token，该access_token为基本接口使用的access_token
        $access_token_arr = $this->getAccessToken();
        //设置模板消息
        $array = array();
        //设置接受消息用户的openid
        $array['touser'] = $openid;
        //设置模板消息id
        $array['template_id'] = $template_id;
        //设置点击模板消息跳转的url,因为我是测试，所以写的是百度
        $array['url'] = env('APP_URL') . '/h5';
        //设置模板消息
        $data = array();
        $data['first'] = array();
        $data['first']['value'] = $data_params['first'];
        $data['first']['color'] = '#173177';
        $data['keyword1']['value'] = $data_params['keyword1'];
        $data['first']['color'] = '#173177';
        $data['keyword2']['value'] = $data_params['keyword2'];
        $data['first']['color'] = '#173177';
        $data['keyword3']['value'] = $data_params['keyword3'];
        $data['first']['color'] = '#173177';
        $data['keyword4']['value'] = $data_params['keyword4'];
        $data['first']['color'] = '#173177';
        $data['remark']['value'] = $data_params['remark'];
        $data['first']['color'] = '#173177';
        $array['data'] = $data;
        //调用公共方法curl_post，发送模板消息
        $data = $this->curl_post($array,'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token_arr);
        return $data;
    }

    //发送模版消息
    // 购买成功  RnWD8sDGTeH99RO_b4XYJC3gSreCBTgVV__zsAeien8
    public function sendActiveMessage($openid, $template_id, $data_params){
        if ( !$openid || !$template_id ) {
            return false;
        }

        //获取access_token，该access_token为基本接口使用的access_token
        $access_token_arr = $this->getAccessToken();
        //设置模板消息
        $array = array();
        //设置接受消息用户的openid
        $array['touser'] = $openid;
        //设置模板消息id
        $array['template_id'] = $template_id;
        //设置点击模板消息跳转的url,因为我是测试，所以写的是百度
        $array['url'] = env('APP_URL') . '/h5';
        //设置模板消息
        $data = array();
        $data['first'] = array();
        $data['first']['value'] = $data_params['first'];
        $data['first']['color'] = '#173177';
        $data['keyword1']['value'] = $data_params['keyword1'];
        $data['first']['color'] = '#173177';
        $data['keyword2']['value'] = $data_params['keyword2'];
        $data['first']['color'] = '#173177';
        $data['remark']['value'] = $data_params['remark'];
        $data['first']['color'] = '#173177';
        $array['data'] = $data;
        //调用公共方法curl_post，发送模板消息
        $data = $this->curl_post($array,'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token_arr);
        return $data;
    }
}
